package com.ddatsh.transaction.core.model;


import java.util.List;
import java.util.Map;
import java.util.Properties;
 

/**
 * 系统/应用程序上下文
 * 
 * @author ddatsh
 * @version 1.0
 */
public class ApplicationContext {
	// 系统参数变量
	private static Properties properties;
	// 交易定义Map
	private static Map<String, TransactionDefinition> transDefMap;
	// 请求响应扩容队列
	private static CapacityExtQueue capacityExtQueue;
	// 容量标准
	private static List<CapacityLimit> capacityLimits;
	// 请求转发规则
	private static List<RedirectRule> redirectRule;
	// 当前处理中的交易数量
	private static int currentTransCount = 0;
	// 服务器IP
	private static String serverIp;
	// 服务器名称
	private static String serverName;

	public static void init(Properties properties, Map<String, TransactionDefinition> transDefMap, CapacityExtQueue capacityExtQueue,
			List<CapacityLimit> capacityLimits, List<RedirectRule> redirectRule) {
		ApplicationContext.properties = properties;
		ApplicationContext.transDefMap = transDefMap;
		ApplicationContext.capacityExtQueue = capacityExtQueue;
		ApplicationContext.capacityLimits = capacityLimits;
		ApplicationContext.redirectRule = redirectRule;
	}

	/**
	 * 克隆系统参数到一个新Map对象
	 * 
	 * @return 克隆后的map对象
	 */
	public static Properties clonePropsToMap() {
		return (Properties) properties.clone();
	}

	/**
	 * 获取系统上下文变量
	 * 
	 * @param name
	 *            系统配置项名称
	 * 
	 * @return 系统配置项值
	 */
	public static String getProperty(String name) {
		return properties.getProperty(name);
	}

	/**
	 * 根据交易代码获取交易定义描述
	 * 
	 * @param transCode
	 * 
	 * @return 交易定义对象
	 */
	public static TransactionDefinition getTransDef(String transCode) {
		return transDefMap.get(transCode);
	}

	/**
	 * 应用参数
	 * 
	 * @return 应用参数
	 */
	public static Properties getProperties() {
		return properties;
	}

	/**
	 * 交易扩展队列
	 * 
	 * @return 交易扩展队列
	 */
	public static CapacityExtQueue getCapacityExtQueue() {
		return capacityExtQueue;
	}

	/**
	 * 当前系统未完成交易数
	 * 
	 * @return 当前交易数
	 */
	public static int currentTransCount() {
		return currentTransCount;
	}

	/**
	 * 当前交易数增加
	 * 
	 */
	public static void currentTransCountAdd() {
		currentTransCount++;
	}

	/**
	 * 当前交易数减少
	 * 
	 */
	public static void currentTransCountReduction() {
		currentTransCount--;
		if (currentTransCount < 0) {
			currentTransCount = 0;
		}
	}

	/**
	 * 返回当前交易扩展队列交易数量
	 * 
	 * @return 交易数量
	 */
	public static int currentCapacityExtCount() {
		return capacityExtQueue.count();
	}

	/**
	 * 通过错误码获取错误信息
	 * 
	 * @param errorCode
	 * @return
	 */
	public static String getErrorMsg(String errorCode) {
		return properties.getProperty(PropertyPrefix.RESPONSE_ERROR_MSG_PREFIX + "." + errorCode);
	}

	/**
	 * 获取服务器IP
	 * 
	 * @return
	 */
	public static String getServerIp() {
		return serverIp;
	}

	/**
	 * 获取服务器名称
	 * 
	 * @return
	 */
	public static String getServerName() {
		return serverName;
	}

	public static void setServerIp(String serverIp) {
		ApplicationContext.serverIp = serverIp;
	}

	public static void setServerName(String serverName) {
		ApplicationContext.serverName = serverName;
	}

}